package json

import (
	"codejy/store"
	"codejy/structdef"
	"codejy/typedefault"
	"codejy/utils"
	"strconv"
	"strings"
)

type JsonArray struct {
	Addr        string
	ThreadStore *structdef.ThreadStore
	Array       []map[string]string
}

// 创建实例
func (a *JsonArray) CreateTypeDefault(addr string, threadStore *structdef.ThreadStore, parameter ...string) {
	s := parameter[0]
	//判断是否是json数组对象
	validJSON := utils.IsValidJSONArray(s)
	if !validJSON {
		panic("不是json,请检查格式")
	}
	jsonArray := utils.JsonToSliceMap(s)
	instance := &JsonArray{
		Addr:        addr,
		ThreadStore: threadStore,
		Array:       jsonArray,
	}
	store.AddTypeDefaultInstanceStore(threadStore, a.GetTypeName(), addr, instance)
}
func (a *JsonArray) ParameterProcessing(parameter []string) []string {
	//排除前后""
	for i := range parameter {
		s := parameter[i]
		if strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"") {
			s = s[1 : len(s)-1]
			parameter[i] = s
		}
	}
	return parameter
}

func (a *JsonArray) GetData() interface{} {
	return a.Array
}

func (a *JsonArray) ToStr() string {
	value1, b1 := typedefault.GetTypeDefaultValue[[]map[string]string](a.ThreadStore, a.GetTypeName(), a.Addr)
	if b1 {
		return ListToJson(value1)
	}
	return ""
}

func ListToJson(maps []map[string]string) string {
	var Str = "["
	for i := range maps {
		Str += utils.JsonToMapStr(maps[i])
		Str += ","
	}
	Str = Str[0 : len(Str)-1]
	Str += "]"
	return Str
}

// 获取数组的长度
func (a *JsonArray) GetLength() string {
	return strconv.Itoa(len(a.Array))
}

// 获取数组的某个元素,返回的是json对象
func (a *JsonArray) Get(index string) string {
	//转为int
	indexInt, p := strconv.Atoi(index)
	if p != nil {
		panic("JsonArray-get(?)参数错误,必须是整数")
	}
	m := a.Array[indexInt]
	return MapToJsonObj(m, a.ThreadStore)
}

// 删除数组的某个元素
func (a *JsonArray) Remove(index string) {
	//转为int
	indexInt, p := strconv.Atoi(index)
	if p != nil {
		panic("JsonArray-del(?)参数错误,必须是整数")
	}
	a.Array = append(a.Array[:indexInt], a.Array[indexInt+1:]...)
}

// 添加元素,参数是json对象字符串
func (a *JsonArray) Add(jsonObj string) {
	//判断是否是json对象
	validJSON := utils.IsValidJSONObject(jsonObj)
	if !validJSON {
		panic("不是json,请检查格式")
	}
	m := utils.JsonToMap(jsonObj)
	a.Array = append(a.Array, m)
}

func (f *JsonArray) GetDef() string {
	return `
		@CodeManual(def="new("JsonArray")",des="创建Json数组转换实例")
		type JsonArray{
			def init{
				core&CreateTypeDefault($1)
			}
			@CodeManual(def="size()",des="获取数组的长度")
			def size{
				to core&GetLength()
			}
			@CodeManual(def="get(index)",des="获取数组的某个元素,返回的是JsonObj实例")
			def get{
				to core&Get($1)
			}
			@CodeManual(def="del(index)",des="删除数组的某个json对象")
			def del{
				core&Remove($1)
			}
			@CodeManual(def="add(JsonObjStr)",des="添加元素,参数是json对象字符串")
			def add{	
				core&Add($1)
			}
			@CodeManual(def="toStr()",des="将Json数组转换为字符串")
			def toStr{
				to core&ToStr()
			}
		}
	`
}
func init() {
	typedefault.RegisterTypeDefault(&JsonArray{})
}

func (f *JsonArray) GetTypeName() string {
	return "JsonArray"
}
func StrToJsonArray(jsonArray string, threadStore *structdef.ThreadStore) string {
	jsonSliceArray := utils.JsonToSliceMap(jsonArray)
	addr := typedefault.TypeDefaultNewInstance("JsonArray", threadStore)
	newList := &JsonArray{
		Addr:        addr,
		ThreadStore: threadStore,
		Array:       jsonSliceArray}

	store.AddTypeDefaultInstanceStore(threadStore, "JsonArray", addr, newList)
	return addr
}
func ListToJsonArray(list []map[string]string, threadStore *structdef.ThreadStore) string {
	addr := typedefault.TypeDefaultNewInstance("JsonArray", threadStore)
	newList := &JsonArray{
		Addr:        addr,
		ThreadStore: threadStore,
		Array:       list}

	store.AddTypeDefaultInstanceStore(threadStore, "JsonArray", addr, newList)
	return addr
}
